package com.javaee.demov2.web.api;

import com.javaee.demov2.persistent.entity.Ticket;
import com.javaee.demov2.service.TicketService;
import com.javaee.demov2.utils.bean.PageResult;
import com.javaee.demov2.utils.bean.SysConstants;
import com.javaee.demov2.utils.response.ResultVo;
import com.javaee.demov2.web.dto.ticket.TicketAddDto;
import com.javaee.demov2.web.dto.ticket.TicketQueryDto;
import com.javaee.demov2.web.dto.ticket.TicketUpdateDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 票据管理api接口类 使用Swagger
 */

// @Api：用于类上，表示该类是Swagger的资源，用于描述API资源的基本信息。
@Api(tags = "票据管理")
@Slf4j
@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Autowired
    TicketService ticketService;

    /**
     * 根据票据id查询票据信息
     */
    // @ApiOperation：用于方法上，描述一个HTTP请求的操作。
    @ApiOperation(value = "获取票据", notes = "根据票据id获取票据对象")
    @GetMapping("/{id}")
    public ResultVo<Ticket> getById(@PathVariable Long id) {
        Ticket ticket = ticketService.getById(id);
        return ResultVo.ok(ticket);
    }

    /**
     * 新增票据
     */
    @ApiOperation(value = "新增票据", notes = "新增票据")
    @PostMapping("")
    public ResultVo add(@RequestBody TicketAddDto ticketAddDto) {
        ticketService.add(ticketAddDto);
        return ResultVo.ok();
    }

    /**
     * 删除票据
     */
    @ApiOperation(value = "删除票据", notes = "根据票据id删除票据对象")
    @DeleteMapping("/{id}")
    public ResultVo delete(@PathVariable Long id) {
        return ticketService.removeById(id)? ResultVo.ok() : ResultVo.fail();
    }

    /**
     * 更新票据信息
     */
    @ApiOperation(value = "更新票据", notes = "更新票据信息")
    @PutMapping("")
    public ResultVo update(@RequestBody TicketUpdateDto ticketUpdateDto) {
        ticketService.update(ticketUpdateDto);
        return ResultVo.ok();
    }

    /**
     * 多条件查询，分页返回查询结果
     */
    @ApiOperation(value = "多条件查询", notes = "多条件查询票据对象")
    @PostMapping("query")
    public ResultVo<PageResult> pageQuery(@RequestBody TicketQueryDto ticketQueryDto) {
        return ResultVo.ok(ticketService.query(ticketQueryDto));
    }

    /**
     * 判断是否存在相同的票据编号接口
     */
    @ApiOperation(value = "是否存在相同的票据编号", notes = "判断是否存在相同的票据")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "票据编号存在返回ture,不存在返回false"),})
    @PostMapping("isExistTicketNumber")
    public ResultVo isExistTicketNumber(@RequestParam String ticketNumber) {
        boolean res = ticketService.isExistTicketNumber(ticketNumber);
        return res? ResultVo.ok(SysConstants.TRUE) : ResultVo.ok(SysConstants.FALSE);
    }

    /**
     * 根据票据状态查询票据列表接口
     */
    @ApiOperation(value = "根据票据状态查询票据列表", notes = "根据传入的票据状态查询符合该状态的票据列表")
    @GetMapping("findByStatus")
    public ResultVo<List<Ticket>> findByStatus(@RequestParam Integer status) {
        return ResultVo.ok(ticketService.findByStatus(status));
    }
}
